<template>
  <div class="user-center">
    <!-- 个人信息卡片 -->
    <el-card class="user-info-card">
      <div class="user-header">
        <el-avatar :size="80" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
        <div class="user-details">
          <div class="user-title">
            <h2 class="username">用户名</h2>
            <el-tag class="member-tag" type="warning">普通会员</el-tag>
          </div>
          <p class="user-id">账号：example@email.com</p>
        </div>
      </div>
    </el-card>

    <!-- 密码修改 -->
    <el-card class="form-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>
      <el-form :model="passwordForm" label-width="100px" :rules="passwordRules" ref="passwordFormRef">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPassword" type="password" placeholder="请输入原密码" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword" type="password" placeholder="请输入新密码" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword" type="password" placeholder="请确认新密码" show-password />
        </el-form-item>
        <el-form-item label="验证码" prop="verifyCode">
          <div class="verify-code-input">
            <el-input v-model="passwordForm.verifyCode" placeholder="请输入验证码">
              <template #append>
                <el-button 
                  class="verify-code-btn"
                  :disabled="isCountingDown" 
                  @click="sendVerifyCode"
                >
                  {{ countDownText }}
                </el-button>
              </template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatePassword">修改密码</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 密码表单
const passwordFormRef = ref(null)
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: '',
  verifyCode: ''
})

// 验证码倒计时
const isCountingDown = ref(false)
const countdown = ref(60)
const countDownText = ref('发送验证码')

// 发送验证码
const sendVerifyCode = async () => {
  try {
    // TODO: 调用发送验证码接口
    ElMessage.success('验证码已发送到您的邮箱')
    isCountingDown.value = true
    countdown.value = 60
    
    const timer = setInterval(() => {
      countdown.value--
      countDownText.value = `${countdown.value}秒后重试`
      
      if (countdown.value <= 0) {
        clearInterval(timer)
        isCountingDown.value = false
        countDownText.value = '发送验证码'
      }
    }, 1000)
  } catch (error) {
    ElMessage.error('验证码发送失败，请重试')
  }
}

// 密码验证规则
const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  verifyCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度应为6位', trigger: 'blur' }
  ]
}

// 更新密码
const updatePassword = async () => {
  if (!passwordFormRef.value) return
  await passwordFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 调用接口更新密码
      ElMessage.success('密码修改成功')
      passwordForm.oldPassword = ''
      passwordForm.newPassword = ''
      passwordForm.confirmPassword = ''
      passwordForm.verifyCode = ''
    }
  })
}
</script>

<style scoped>
.user-center {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.user-info-card {
  margin-bottom: 24px;
}

.user-header {
  display: flex;
  align-items: center;
  gap: 24px;
}

.user-details {
  flex: 1;
}

.user-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.username {
  margin: 0;
  font-size: 24px;
  color: #1e293b;
}

.member-tag {
  font-weight: 500;
}

.user-id {
  margin: 8px 0 0;
  color: #64748b;
  font-size: 14px;
}

.card-header {
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #1e293b;
  font-weight: 500;
}

.form-card {
  margin-bottom: 24px;
}

.form-card :deep(.el-form-item__label) {
  font-weight: 500;
}

.form-card :deep(.el-input) {
  max-width: 400px;
}

.verify-code-input {
  display: flex;
  align-items: center;
  max-width: 400px;
}

.verify-code-input :deep(.el-input-group__append) {
  padding: 0;
  background: none;
}

.verify-code-input :deep(.verify-code-btn) {
  margin: 0;
  height: 32px;
  padding: 0 15px;
  font-size: 14px;
  white-space: nowrap;
  min-width: 102px;
}

.verify-code-input :deep(.el-input__wrapper) {
  width: 298px;
}

@media (max-width: 768px) {
  .user-center {
    padding: 0 16px;
  }

  .user-header {
    gap: 16px;
  }

  .username {
    font-size: 18px;
  }

  .user-id {
    font-size: 13px;
  }

  .member-tag {
    transform: scale(0.9);
    transform-origin: left center;
  }

  .form-card {
    padding: 12px;
  }

  .form-card :deep(.el-form-item__label) {
    font-size: 14px;
    padding-right: 12px;
  }

  .form-card :deep(.el-form) {
    max-width: 100%;
  }

  .form-card :deep(.el-input) {
    max-width: 100%;
  }

  /* 验证码输入框移动端适配 */
  .verify-code-input {
    max-width: 100%;
  }

  .verify-code-input :deep(.el-input__wrapper) {
    width: auto;
  }

  .verify-code-input :deep(.verify-code-btn) {
    min-width: 90px;
    font-size: 13px;
    padding: 0 8px;
  }

  .verify-code-input :deep(.el-input-group__append) {
    padding: 0;
  }

  .verify-code-input :deep(.el-button) {
    height: 32px;
  }
}
</style> 